﻿package mortal.game.view.halidom
{
    import Message.Client.*;
    import Message.DB.Tables.*;
    import Message.Game.*;
    import __AS3__.vec.*;
    import baseEngine.modifiers.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.primitives.object2d.*;
    import frEngine.render.layer.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.rotate3dModelSprite.*;
    import mortal.component.window.*;
    import mortal.component.windowTip.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.halidom.render.*;
    import mortal.game.view.halidom.view.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class HalidomModuleNew extends Window
    {
        private var _bg:GImageBitmap;
        private var _rect3d:Rect3DObject;
        private var _img2D:Img2D;
        private var _effectPlayer:EffectPlayer;
        private var _attrBg:GBitmap;
        private var _combBg:GBitmap;
        private var _name:GTextFiled;
        private var _comb:BitmapNumberText;
        private var _item:BaseItem;
        private var _num:GTextFiled;
        private var _findTxt:GTextFiled;
        private var _advanceBtn:GLoadingButton;
        private var _attrSprite:Sprite;
        private var _addAttrSprite:Sprite;
        private var _rotate3d:Rotate3dModelSprite;
        private var _attrName:Vector.<GTextFiled>;
        private var _attr:Vector.<GTextFiled>;
        private var _addAttr:Vector.<GTextFiled>;
        private var _arrowArr:Vector.<GBitmap>;
        private var _renders:Vector.<HalidomRendererNew>;
        private var _pos:Array;
        private var _isEnoughMaterial:Boolean;
        private var _ckIntervalTime:int = 0;
        private var _selectIndex:int;

        public function HalidomModuleNew(param1:ILayer = null)
        {
            this._attrName = new Vector.<GTextFiled>;
            this._attr = new Vector.<GTextFiled>;
            this._addAttr = new Vector.<GTextFiled>;
            this._arrowArr = new Vector.<GBitmap>;
            this._renders = new Vector.<HalidomRendererNew>;
            this._pos = [new Point(115, 421 - 60), new Point(5, 326 - 60), new Point(5, 184 - 60), new Point(115, 92 - 60), new Point(247, 70 - 60), new Point(370, 70 - 60), new Point(502, 92 - 60), new Point(616, 184 - 60), new Point(616, 326 - 60), new Point(502, 421 - 60)];
            super(param1);
            setSize(763, 558);
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        private function onSubmitAllBtnClick(event:MouseEvent) : void
        {
            var onCloseHandler:Function;
            var e:* = event;
            onCloseHandler = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.HalidomOper, {type:EHalidomOperType._EHalidomOperTypeSubmit, id:0}));
                }
                return;
            }// end function
            ;
            Alert.show(Language.getString(70424), null, Alert.OK | Alert.CANCEL, null, onCloseHandler);
            return;
        }// end function

        private function selectBarByItemCode(event:DataEvent) : void
        {
            return;
        }// end function

        public function onHalidomInfoRes(param1:Object = null) : void
        {
            var _loc_2:Dictionary = null;
            var _loc_3:THalidomConfig = null;
            var _loc_4:int = 0;
            var _loc_5:SHalidom = null;
            var _loc_6:int = 0;
            if (!Boolean(param1))
            {
                this.setRendersData();
                _loc_2 = Cache.instance.halidom.data;
                for each (_loc_5 in _loc_2)
                {
                    
                    _loc_3 = HalidomConfig.instance.getItem(_loc_5.halidomId, (_loc_5.level + 1));
                    if (_loc_3)
                    {
                        _loc_4 = ItemsUtil.getUnbindCode(_loc_3.itemCode);
                        _loc_6 = Cache.instance.pack.backPackCache.getItemCountByItemCode(_loc_4);
                        if (_loc_6 >= _loc_3.itemNum)
                        {
                            this._selectIndex = _loc_3.halidomId - 1;
                            break;
                        }
                    }
                }
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.HalidomSelectItem, this._selectIndex));
            this.setTotalAttr();
            return;
        }// end function

        private function onLinkTxt(event:TextEvent) : void
        {
            var _loc_3:String = null;
            var _loc_4:Object = null;
            var _loc_5:Array = null;
            var _loc_2:* = this._renders[this._selectIndex].data;
            if (this._findTxt.text == Language.getString(70427))
            {
                if (_loc_2.maps.indexOf(":") > -1)
                {
                    _loc_5 = _loc_2.maps.split(":");
                    _loc_3 = _loc_5[0];
                    _loc_4 = _loc_5[1];
                }
                else
                {
                    _loc_3 = _loc_2.maps;
                }
                Dispatcher.dispatchEvent(new DataEvent(_loc_3, _loc_4));
            }
            else
            {
                HalidomWayFindingView.instance.show();
                HalidomWayFindingView.instance.maps = _loc_2.maps;
            }
            return;
        }// end function

        private function onadvance(event:MouseEvent) : void
        {
            if (getTimer() - this._ckIntervalTime < 500)
            {
                return;
            }
            this._ckIntervalTime = getTimer();
            if (!this._isEnoughMaterial)
            {
                MsgManager.showRollTipsMsg(Language.getString(21513), MsgRollTipsType.msgRollTips3);
                return;
            }
            var _loc_2:* = this._renders[this._selectIndex].data;
            Dispatcher.dispatchEvent(new DataEvent(EventName.HalidomOper, {type:EHalidomOperType._EHalidomOperTypeSubmit, id:_loc_2.halidomId}));
            return;
        }// end function

        private function onAdvanceMOver(event:MouseEvent) : void
        {
            this._addAttrSprite.visible = true;
            return;
        }// end function

        private function onAdvanceMOut(event:MouseEvent) : void
        {
            this._addAttrSprite.visible = false;
            return;
        }// end function

        private function onHalidomSelectItem(event:DataEvent) : void
        {
            var _loc_10:int = 0;
            var _loc_12:GTextFiled = null;
            var _loc_13:GTextFiled = null;
            var _loc_14:int = 0;
            this._selectIndex = event.data as int;
            var _loc_2:* = this._renders[this._selectIndex].data;
            var _loc_3:* = this._renders[this._selectIndex].sHalidom;
            if (_loc_3 == null)
            {
                return;
            }
            var _loc_4:* = HalidomConfig.instance.getItem(_loc_3.halidomId, _loc_3.level);
            var _loc_5:* = HalidomConfig.instance.getItem(_loc_3.halidomId, (_loc_3.level + 1));
            if (_loc_5 == null)
            {
                this._advanceBtn.filterEnabled = false;
                _loc_5 = HalidomConfig.instance.getItem(_loc_3.halidomId, _loc_3.level);
            }
            else
            {
                this._advanceBtn.filterEnabled = true;
            }
            var _loc_6:* = ItemsUtil.getUnbindCode(_loc_5.itemCode);
            var _loc_7:* = Cache.instance.pack.backPackCache;
            var _loc_8:* = _loc_7.getItemCountByItemCode(_loc_6);
            this._item.itemCode = _loc_5.itemCode;
            this._item.amount = _loc_8;
            if (_loc_8 >= _loc_5.itemNum)
            {
                this._isEnoughMaterial = true;
                this._num.textColor = GlobalStyle.colorLvUint;
            }
            else
            {
                this._isEnoughMaterial = false;
                this._num.textColor = GlobalStyle.colorHongUint;
            }
            this._num.text = "*" + _loc_5.itemNum;
            this._name.htmlText = Language.getStringByParam(70428, _loc_3.level, _loc_2.description);
            var _loc_9:* = _loc_2.maps.indexOf("#") > 0 ? (Language.getString(70411)) : (Language.getString(70427));
            this._findTxt.htmlText = HTMLUtil.addLink(_loc_9, GlobalStyle.colorLv);
            this.cleanAttr();
            this.add3DModle(_loc_2);
            var _loc_11:int = 0;
            while (_loc_11 < 4)
            {
                
                _loc_12 = this._attr[_loc_11];
                _loc_13 = this._addAttr[_loc_11];
                _loc_14 = int(_loc_13.text) - int(_loc_12.text);
                if (_loc_14 > 0)
                {
                    if (int(_loc_12.text) == 0)
                    {
                        _loc_12.text = "0";
                    }
                    this._addAttr[_loc_11].text = _loc_14.toString();
                    this._arrowArr[_loc_11].visible = true;
                    _loc_10++;
                }
                else
                {
                    _loc_13.text = "";
                    this._arrowArr[_loc_11].visible = false;
                }
                _loc_11++;
            }
            var _loc_15:* = _loc_10 > 2 ? (462) : (474);
            this._addAttrSprite.y = _loc_10 > 2 ? (462) : (474);
            this._attrSprite.y = _loc_15;
            return;
        }// end function

        private function setTotalAttr() : void
        {
            var _loc_3:THalidomConfig = null;
            var _loc_4:SHalidom = null;
            var _loc_5:int = 0;
            var _loc_6:String = null;
            var _loc_7:String = null;
            var _loc_1:* = Cache.instance.halidom.data;
            var _loc_2:* = new Dictionary();
            for each (_loc_4 in _loc_1)
            {
                
                _loc_3 = HalidomConfig.instance.getItem(_loc_4.halidomId, _loc_4.level);
                if (_loc_3)
                {
                    for each (_loc_7 in AttributeUtil.attrs)
                    {
                        
                        if (_loc_3.hasOwnProperty(_loc_7) && int(_loc_3[_loc_7]) > 0)
                        {
                            _loc_2[_loc_7] = int(_loc_2[_loc_7]) + int(_loc_3[_loc_7]);
                        }
                    }
                }
            }
            for (_loc_6 in _loc_2)
            {
                
                _loc_5 = _loc_5 + CombatUtil.getCombatValueByName(_loc_6, _loc_2[_loc_6]);
            }
            this._comb.text = _loc_5.toString();
            return;
        }// end function

        private function setRendersData() : void
        {
            var _loc_3:HalidomRendererNew = null;
            var _loc_1:* = HalidomConfig.instance.seriesData;
            var _loc_2:* = _loc_1.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2)
            {
                
                _loc_3 = this._renders[_loc_4];
                _loc_3.data = _loc_1[_loc_4];
                _loc_4++;
            }
            return;
        }// end function

        private function cleanAttr() : void
        {
            var _loc_1:int = 0;
            while (_loc_1 < 4)
            {
                
                GTextFiled(this._attr[_loc_1]).text = "";
                GTextFiled(this._addAttr[_loc_1]).text = "";
                GTextFiled(this._attrName[_loc_1]).text = "";
                this._arrowArr[_loc_1].visible = false;
                _loc_1++;
            }
            return;
        }// end function

        private function onBgLoadCom() : void
        {
            var loadUrlCompl:Function;
            loadUrlCompl = function (param1) : void
            {
                _img2D = new Img2D(null, param1.bitmapData, new Rectangle(7, 7, 740, 510), false);
                _rect3d.addImg(_img2D, Layer3DManager.backGroudMaskLayer);
                return;
            }// end function
            ;
            LoaderManager.instance.load(ImagesConst.HalidomBg + ".swf", loadUrlCompl);
            return;
        }// end function

        private function add3DModle(param1:THalidomSeriesConfig) : void
        {
            if (this._effectPlayer)
            {
                this._effectPlayer.dispose();
            }
            this._rect3d.disposeObj3d(this._effectPlayer);
            this._effectPlayer = EffectPlayerPool.instance.getEffectPlayer(param1.effect, null, this._rect3d.renderList, false);
            this._effectPlayer.hangBoneName = BoneConst.MountHangModelBone;
            this._effectPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
            var _loc_2:Number = 2.8;
            this._effectPlayer.scaleZ = 2.8;
            var _loc_2:* = _loc_2;
            this._effectPlayer.scaleY = _loc_2;
            this._effectPlayer.scaleX = _loc_2;
            this._rect3d.addObject3d(this._effectPlayer, 365, 262);
            this._rotate3d.model = this._effectPlayer;
            return;
        }// end function

        private function onResCompl() : void
        {
            this._attrBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.HalidomAttrBg);
            this._combBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.HalidomCombBg);
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.HalidomTitle, 350, 2, this));
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:HalidomRendererNew = null;
            super.createDisposedChildrenImpl();
            this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(12 + 5, 29 + 5, 740, 510), this);
            Rect3DManager.instance.windowShowHander(null, this);
            this.pushUIToDisposeVec(UIFactory.bg(2, -18, 775, 610, this, ImagesConst.WindowBg3));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.WindowTitleBg, (763 - 257) * 0.5 + 2, -52, this));
            this._bg = UIFactory.gImageBitmap(ImagesConst.HalidomBg, 12, 29, this, this.onBgLoadCom);
            this._rotate3d = UIFactory.getUICompoment(Rotate3dModelSprite, 167, 200, contentTopOf3DSprite);
            this._rotate3d.setSize(440, 195);
            this._attrBg = UIFactory.gBitmap("", 260, 458, contentTopOf3DSprite);
            this._combBg = UIFactory.gBitmap("", 422, 218, contentTopOf3DSprite);
            this._name = UIFactory.gTextField("", 272, 420, 220, 30, contentTopOf3DSprite, GlobalStyle.textFormatLv.setSize(16).center().setBold(true));
            this._comb = UIFactory.bitmapNumberText(532, 225, "GroupCopyNum.png", 31, 35, -16, contentTopOf3DSprite, 13, BitmapNumberText.Mid);
            pushUIToDisposeVec(UIFactory.gTextField(Language.getString(21514), 244, 510, 60, 20, contentTopOf3DSprite));
            this._attrSprite = UIFactory.sprite(265, 462, contentTopOf3DSprite);
            this._addAttrSprite = UIFactory.sprite(265, 462, contentTopOf3DSprite);
            this._addAttrSprite.visible = false;
            var _loc_1:int = 0;
            while (_loc_1 < 10)
            {
                
                _loc_4 = UIFactory.getUICompoment(HalidomRendererNew);
                _loc_4.pos = this._pos[_loc_1] as Point;
                _loc_4.rect3d = this._rect3d;
                this._renders.push(_loc_4);
                contentTopOf3DSprite.addChild(_loc_4);
                _loc_1++;
            }
            _loc_1 = 0;
            while (_loc_1 < 4)
            {
                
                _loc_2 = _loc_1 % 2;
                _loc_3 = Math.floor(_loc_1 / 2);
                this._attrName.push(UIFactory.gTextField("", 0 + _loc_2 * 125, _loc_3 * 23, 80, 20, this._attrSprite));
                this._attr.push(UIFactory.gTextField("", 40 + _loc_2 * 125, _loc_3 * 23, 80, 20, this._attrSprite));
                this._addAttr.push(UIFactory.gTextField("", 85 + _loc_2 * 125, _loc_3 * 23, 80, 20, this._addAttrSprite));
                this._arrowArr.push(UIFactory.gBitmap(ImagesConst.upgradeArrow, 75 + _loc_2 * 125, _loc_3 * 23 + 2, this._addAttrSprite));
                _loc_1++;
            }
            this._item = UIFactory.baseItem(254, 532, 64, 64, contentTopOf3DSprite);
            this._item.setItemStyle();
            this._num = UIFactory.gTextField("", 305, 544, 25, 20, contentTopOf3DSprite);
            this._findTxt = UIFactory.gTextField("", 490, 552, 80, 20, contentTopOf3DSprite);
            this._advanceBtn = UIFactory.gLoadingButton(ResFileConst.HalidomAdvanceBtn, 338, 515, 106, 60, contentTopOf3DSprite);
            _closeBtn.parent.addChild(_closeBtn);
            this._findTxt.configEventListener(TextEvent.LINK, this.onLinkTxt);
            this._advanceBtn.configEventListener(MouseEvent.CLICK, this.onadvance);
            this._advanceBtn.configEventListener(MouseEvent.MOUSE_OVER, this.onAdvanceMOver);
            this._advanceBtn.configEventListener(MouseEvent.MOUSE_OUT, this.onAdvanceMOut);
            Dispatcher.addEventListener(EventName.HalidomSelectItem, this.onHalidomSelectItem);
            Dispatcher.addEventListener(EventName.HalidomSelectTabByItemCode, this.selectBarByItemCode);
            NetDispatcher.addCmdListener(ServerCommand.HalidomInfoRes, this.onHalidomInfoRes);
            LoaderHelp.addResCallBack(ResFileConst.halidom, this.onResCompl);
            Dispatcher.dispatchEvent(new DataEvent(EventName.HalidomInfoReq));
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            var _loc_2:HalidomRendererNew = null;
            super.disposeImpl(param1);
            LoaderHelp.removeResEvent(ResFileConst.halidom, this.onResCompl);
            Dispatcher.removeEventListener(EventName.HalidomSelectItem, this.onHalidomSelectItem);
            Dispatcher.removeEventListener(EventName.HalidomSelectTabByItemCode, this.selectBarByItemCode);
            NetDispatcher.removeCmdListener(ServerCommand.HalidomInfoRes, this.onHalidomInfoRes);
            UIFactory.disposeSprite(this._attrSprite, true);
            UIFactory.disposeSprite(this._addAttrSprite, true);
            for each (_loc_2 in this._renders)
            {
                
                _loc_2.dispose(param1);
            }
            this._attr.splice(0, this._attr.length);
            this._addAttr.splice(0, this._addAttr.length);
            this._attrName.splice(0, this._attrName.length);
            this._arrowArr.splice(0, this._arrowArr.length);
            this._renders.splice(0, this._renders.length);
            this._name.dispose(param1);
            this._comb.dispose(param1);
            this._attrBg.dispose(param1);
            this._combBg.dispose(param1);
            this._bg.dispose(param1);
            this._item.dispose(param1);
            this._num.dispose(param1);
            this._findTxt.dispose(param1);
            this._advanceBtn.dispose(param1);
            this._rotate3d.dispose(param1);
            WindowTipManager.dispose(WindowTipType.HALIDOM_TOTALATTR);
            Rect3DManager.instance.disposeRect3d(this._rect3d);
            this._rect3d = null;
            this._img2D = null;
            this._effectPlayer = null;
            this._isEnoughMaterial = false;
            this._selectIndex = 0;
            this._name = null;
            this._comb = null;
            this._attrBg = null;
            this._combBg = null;
            this._bg = null;
            this._item = null;
            this._num = null;
            this._findTxt = null;
            this._advanceBtn = null;
            this._rotate3d = null;
            return;
        }// end function

    }
}
